package edu.hawaii.duedates;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Checks various libraries and retrieves information on books that the patron has checked out.
 * 
 * @author Erin Kim and Arthur Shum
 */
public class DueDates {

  /**
   * Temp function that will stop the PMD Singleton error.
   */
  public void wthSingleton() {
    int i = 1;
  }

  /**
   * Creates an instance of UhLibrary and returns a string containing the information on checked out
   * books.
   * 
   * @param bannerId Contains the Banner ID of a patron.
   * @param lastName Last name of the patron.
   * @return A ready-to-print string containing info about checked-out items.
   * @throws Exception If an error occurs while retrieving info.
   */
  protected static String getUhInfo(String bannerId, String lastName) throws Exception {
    Library uhSite = new UhLibrary(bannerId, lastName);
    return uhSite.getInfo();
  }

  /**
   * Creates an instance of the Hawaii State Public Library and returns a string containing the
   * information on checked out books.
   * 
   * @param barcode Contains the barcode of the patron
   * @param phoneNumber Contains the last 4 digits of the patron's phone number
   * @return A ready-to-print string containing infor about checked-out items.
   * @throws Exception If an error occurs while retrieving info
   */
  protected static String getHawaiiInfo(String barcode, String phoneNumber) throws Exception {
    Library hawaiiSite = new HawaiiStateLibrary(barcode, phoneNumber);
    return hawaiiSite.getInfo();
  }

  /**
   * Generates the text-menu interface, gets user input, and calls the appropriate method to
   * retrieve information on the patron's checked out books.
   * 
   * @param params Three strings: site, arg1, arg2.
   * @throws Exception If problems occur accessing the library sites.
   */
  public static void main(String[] params) throws Exception {

    // Check UH Manoa Library
    if (params.length == 0) {
      String site = "";

      InputStreamReader in = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(in);

      System.out.println("\n******************************************");
      System.out.println("Hello and welcome to the DueDates Program");
      System.out.println("Please select a site from the following options:");
      System.out.println("1. University of Hawaii Library");
      System.out.println("2. Hawaii State Public Library");
      System.out.println("3. Exit");
      System.out.println("******************************************");
      System.out.print("Your choice: ");

      site = br.readLine();

      if ("1".equals(site)) {
        String bannerId = "";
        String lastName = "";

        System.out.print("\n\nPlease enter your UH Banner ID: ");
        bannerId = br.readLine();
        System.out.print("\nPlease enter your Last name: ");
        lastName = br.readLine();
        System.out.println("\nProcessing...");

        System.out.println(getUhInfo(bannerId, lastName));
      }
      else if ("2".equals(site)) {
        String barcode = "";
        String phoneNumber = "";

        System.out.print("\n\nPlease enter your barcode: ");
        barcode = br.readLine();
        System.out.print("\nPlease enter the last 4 digits of your phone number: ");
        phoneNumber = br.readLine();
        System.out.println("\nProcessing...");

        System.out.println(getHawaiiInfo(barcode, phoneNumber));

      }
      else if ("3".equals(site)) {
        System.out.println("\nThank you");
      }
    } // if
    else {
      String sites = params[0];

      if ("uh".equals(sites)) {
        String bannerId = params[1];
        String lastName = params[2];

        System.out.println("Processing information for site: 'University of Hawaii Library'... ");
        System.out.println(getUhInfo(bannerId, lastName));
      }
      else if ("hpl".equals(sites)) {
        String barcode = params[1];
        String phoneNumber = params[2];

        System.out.println("Processing information for site: 'Hawaii State Public Library'... ");
        System.out.println(getHawaiiInfo(barcode, phoneNumber));
      }
    } // else

    // Check the Hawaii State Library.
    /*
     * Library hsl1 = new HawaiiStateLibrary("0011429495", "3845"); String hsl1Results =
     * hsl1.getInfo(); System.out.print(hsl1Results);
     * 
     * Library hsl2 = new HawaiiStateLibrary("0013433719", "0769"); String hsl2Results =
     * hsl2.getInfo(); System.out.print(hsl2Results);
     */
  }
}
